﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using YGWebMobileApi.Data;
using OneMile.Common;
using OneMile.Entity.Model;
using OneMile.BLL;

namespace YGWebMobileApi.APIs
{
    [MobileAPI(Code = "MC0003", Name = "用户添加收藏")]
    public class UAddNewFavorite : IProcessMessage
    {
        private BLLUFavorites bllUFav;
        public BLLUFavorites BllUFav
        {
            get
            {
                if (bllUFav == null)
                    bllUFav = BLLFactory.getInstance().CreateUFavoritesBLL();
                return bllUFav;
            }
        }


        #region IProcessMessage 成员

        public MobileRspMsg ProcessMessage(MobileReqMsg reqMsg)
        {
            try
            {
                MobileRspMsg rspMsg = new MobileRspMsg(reqMsg);
                // 获取参数字段（具体请参见文档）
                string userid = reqMsg.Body.GetFieldValue("UserId");
                string shopid = reqMsg.Body.GetFieldValue("ShopsId");
              
                if (string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(shopid))
                {
                    rspMsg.Head.RspCode = MessageCode.Paramter_Empty.Code;
                    rspMsg.Head.RspMsg = MessageCode.Paramter_Empty.Msg;
                    return rspMsg;
                }

                if (BllUFav.IsFavoriteExit(userid, shopid))
                {
                    rspMsg.Head.RspCode = MessageCode.Favorite_Already_Exit.Code;
                    rspMsg.Head.RspMsg = MessageCode.Favorite_Already_Exit.Msg;

                    return rspMsg;
                }
                else
                {
                    EUserCollection fav = new EUserCollection();
                    fav.UserCollectionId = Guid.NewGuid();
                    fav.UserId = new Guid(userid);
                    fav.ShopsId = new Guid(shopid);
                    fav.Disabled = 0;
                    if (BllUFav.AddNewFavorite(fav))
                    {
                        rspMsg.Head.RspCode = MessageCode.Success.Code;
                        rspMsg.Head.RspMsg = MessageCode.Success.Msg;

                        return rspMsg;
                    }
                    else
                    {
                        rspMsg.Head.RspCode = MessageCode.Server_Error.Code;
                        rspMsg.Head.RspMsg = MessageCode.Server_Error.Msg;

                        return rspMsg;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorLogWriter.Write("UAddNewFavorite-" + e);
                throw e;
            }
        #endregion
        }
    }
}